package cx.mybatis.session;

import cx.mybatis.bingding.MapperRegistry;
import cx.mybatis.datasource.druid.DruidDataSourceFactory;
import cx.mybatis.mapping.Environment;
import cx.mybatis.mapping.MappedStatement;
import cx.mybatis.transaction.jdbc.JdbcTransactionFactory;
import cx.mybatis.type.TypeAliasRegistry;

import java.util.HashMap;
import java.util.Map;

/**
 * Mybatis全局配置类
 * @Author:cxy
 * @Date:2022/4/23 14:43
 */
public class Configuration {

    // Mapper注册
    protected MapperRegistry mapperRegistry = new MapperRegistry();

    // 存放Sql映射语句的Map
    protected final Map<String, MappedStatement> mappedStatements = new HashMap<>();

    //环境
    protected Environment environment;

    // 类型别名注册机
    protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();

    public Configuration() {
        typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
        typeAliasRegistry.registerAlias("DRUID", DruidDataSourceFactory.class);
    }

    public <T> void addMapper(Class<T> type){
        mapperRegistry.addMapper(type);
    }

    public void addMappedStatement(MappedStatement mappedStatement){
        mappedStatements.put(mappedStatement.getId(),mappedStatement);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return mapperRegistry.getMapper(type, sqlSession);
    }

    public MappedStatement getMappedStatement(String statement) {
        return mappedStatements.get(statement);
    }
}
